function loadSelectAsync(url,data,jquery,tip,code,value){
    AsyncGet(url, data, function(data){
        $(jquery).empty();
        var i=0;
        if(tip!=null){
            $(jquery).append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $(jquery).append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $(jquery).append(option);
        }
    });
}

function loadSelectSync(url,data,jquery,tip,code,value){
    SyncGet(url, data, function(data){
        $(jquery).empty();
        var i=0;
        if(tip!=null){
            $(jquery).append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $(jquery).append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $(jquery).append(option);
        }
    });
}

//根据ID初始化下拉框
function loadSelectById(url,data,id,tip,code,value){
    AsyncGet(url, data, function(data){
        $("#"+id).empty();
        var i=0;
        if(tip!=null){
            $("#"+id).append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $("#"+id).append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $("#"+id).append(option);
        }
    });
}

//根据ID初始化下拉框
function loadSelectByIdAsync(url,data,id,tip,code,value){
    AsyncGet(url, data, function(data){
        $("#"+id).empty();
        var i=0;
        if(tip!=null){
            $("#"+id).append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $("#"+id).append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $("#"+id).append(option);
        }
    });
}

//根据class初始化下拉框
function loadSelectByClassAsync(url,data,className,tip,code,value){
    code=code||"code";
    value=value||"value";
    AsyncGet(url, data, function(data){
        $("."+className).empty();
        var i=0;
        if(tip!=null){
            $("."+className).append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $("."+className).append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $("."+className).append(option);
        }
    });
}

//根据class初始化下拉框
function loadSelectByClassSync(url,data,className,tip,code,value){
    code=code||"code";
    value=value||"value";
    SyncGet(url, data, function(data){
        $("."+className).empty();
        var i=0;
        if(tip!=null){
            $("."+className).append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $("."+className).append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $("."+className).append(option);
        }
    });
}

//根据name初始化下拉框
function loadSelectByNameSync(url,data,name,tip,code,value){
    code=code||"code";
    value=value||"value";
    SyncGet(url, data, function(data){
        $("[name="+name+"]").empty();
        var i=0;
        if(tip!=null){
            $("[name="+name+"]").append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $("[name="+name+"]").append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $("[name="+name+"]").append(option);
        }
    });
}

//根据name初始化下拉框
function loadSelectByNameAsync(url,data,name,tip,code,value){
    code=code||"code";
    value=value||"value";
    AsyncGet(url, data, function(data){
        $("[name="+name+"]").empty();
        var i=0;
        if(tip!=null){
            $("[name="+name+"]").append("<option value selected>"+tip+"</option>");
        }else{
            if(data==null||data.length==0) return;
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).prop("selected",true);
            $(option).text(data[i][value]);
            $("[name="+name+"]").append(option);
            i++;
        }
        for(;i<data.length;i++){
            var option=$("<option></option>");
            $(option).attr("value",data[i][code]);
            $(option).text(data[i][value]);
            $("[name="+name+"]").append(option);
        }
    });
}

$.fn.extend({
    loadSelect2:function (url,placeholder,minimumInputLength,searchName,OutName,multiple) {
        $(this).select2({
            language:"zh-CN",
            ajax: {
                url: url,
                dataType: 'json',
                delay: 250,
                type: 'GET',
                data: function (params) {
                    var object={};
                    object[searchName]=params.term;
                    return object;
                },
                processResults: function (data, params) {
                    // parse the results into the format expected by Select2
                    // since we are using custom formatting functions we do not need to
                    // alter the remote JSON data, except to indicate that infinite
                    // scrolling can be used
                    return {
                        results: data,
                    };
                },
                cache: true
            },
            placeholder: placeholder,
            multiple: multiple||false,
            allowClear: true,
            escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
            minimumInputLength: minimumInputLength,
            templateResult: function formatRepo (repo) {
                if (repo.loading) return repo.text;

                var markup =  repo[OutName];

                return markup;
            }, // omitted for brevity, see the source of this page
            templateSelection: function formatRepoSelection (repo) {
                return repo[OutName] || repo.text;
            }// omitted for brevity, see the source of this page
        });
    }
})
